﻿package pw.archipelag.loaderGame.panel {
	import flash.text.TextField;
	import flash.display.Shape;
	import base.dspObj.DoOperations;
	import base.tf.TextFieldUtils;
	import base.btn.BtnHitEvent;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Rectangle;
	import pw.archipelag.loaderGame.interfaceGame.skinsPanel.SkinBg;
	import pw.archipelag.loaderGame.interfaceGame.skinsPanel.SkinTf;
	import pw.archipelag.loaderGame.interfaceGame.skinsPanel.SkinBtnPanel;
	import pw.archipelag.loaderGame.interfaceGame.skinsPanel.SkinScores;
	import pw.archipelag.loaderGame.interfaceGame.skinsPanel.SkinHelp;
	import flash.geom.Point;
	import flash.display.DisplayObject;

	public class PanelStartEndGame extends PanelAbstractBg {
		
		private static const MARGIN_X_STAGE_DEFAULT: Number = 50;
		private static const MARGIN_Y_STAGE_DEFAULT: Number = 30;
		
		private var _isShowHelpScores: uint;
		private var tfLabel: TextField;
		private var btnPlay: BtnPanel;
		private var btnHelp: BtnPanel;
		private var btnScores: BtnPanel;
		private var panelHelp: PanelHelp;
		private var panelScores: PanelScores;
		private var objPanel: Object;
		private var btnStage: Shape;
		
		public function PanelStartEndGame(isShowHelpScores: uint = 0, nameGame: String = "Gra Archipelag", classMcHelp: Class = null, scorePoints: int = -1, scorePercent: int = -1): void {
			super(1);
			this.createTfLabel(nameGame);
			this.createBtns();
			this.createPanelHelpScores(classMcHelp, scorePoints, scorePercent);
			this.createBtnStage();
			this.isShowHelpScores = isShowHelpScores;
			this.alpha = 0;
			DoOperations.hideShow(this, 1);
			if (this.stage) this.draw();
		}
		
		private function createTfLabel(nameGame: String): void {
			this.tfLabel = TextFieldUtils.createTextField((new SkinTf()).tFormat);
			this.tfLabel.text = nameGame;
			this.addChild(this.tfLabel)
		}
		
		private function removeTfLabel(): void {
			this.removeChild(this.tfLabel);
			this.tfLabel = null;
		}
		
		private function createBtns(): void {
			this.btnPlay = new BtnPanel("GRAJ");
			this.btnPlay.addEventListener(BtnHitEvent.CLICKED, this.closeAndStartOrContinueGame);
			this.addChild(this.btnPlay);
			
			this.btnHelp = new BtnPanel("POMOC");
			this.btnHelp.alpha = 0;
			this.btnHelp.addEventListener(BtnHitEvent.CLICKED, this.showPanelHelp);
			this.addChild(this.btnHelp);
			
			this.btnScores = new BtnPanel("NAJLEPSI");
			this.btnScores.alpha = 0;
			this.btnScores.addEventListener(BtnHitEvent.CLICKED, this.showPanelScores);
			this.addChild(this.btnScores);
		}
		
		private function removeBtns(): void {
			this.btnPlay.removeEventListener(BtnHitEvent.CLICKED, this.closeAndStartOrContinueGame);
			this.btnPlay.destroy();
			this.removeChild(this.btnPlay);
			this.btnPlay = null;
			
			this.btnHelp.removeEventListener(BtnHitEvent.CLICKED, this.showPanelHelp);
			this.btnHelp.destroy();
			this.removeChild(this.btnHelp);
			this.btnHelp = null;
			
			this.btnScores.removeEventListener(BtnHitEvent.CLICKED, this.showPanelScores);
			this.btnScores.destroy();
			this.removeChild(this.btnScores);
			this.btnScores = null;
		}
		
		private function createPanelHelpScores(classMcHelp: Class, scorePoints: int = -1, scorePercent: int = -1): void {
			this.panelHelp = new PanelHelp(classMcHelp);
			this.panelScores = new PanelScores(scorePoints, scorePercent);
			this.objPanel = {panelHelp: this.panelHelp, panelScores: this.panelScores};
			for (var strPanel: String in this.objPanel) {
				var panel: Sprite = Sprite(this.objPanel[strPanel]);
				panel.x = PanelAbstractBg.MARGIN_X_ELEMENTS;
				panel.y = PanelAbstractBg.MARGIN_Y_ELEMENTS + 5;
				panel.alpha = 0;
				this.addChild(panel);
			}
		}
		
		private function showPanelHelp(e: BtnHitEvent): void {
			this.isShowHelpScores = 0;
		}
		
		private function showPanelScores(e: BtnHitEvent): void {
			this.isShowHelpScores = 1;
		}
		
		public function get isShowHelpScores(): uint {
			return this._isShowHelpScores;
		}
		
		public function set isShowHelpScores(value: uint): void {
			this._isShowHelpScores = value;
			var btnToHide: BtnPanel = [this.btnHelp, this.btnScores][value];
			var btnToShow: BtnPanel = [this.btnScores, this.btnHelp][value];
			this.setChildIndex(btnToShow, this.numChildren - 1);
			btnToHide.removeMouseEvents();
			DoOperations.hideShow(btnToHide, 0);
			btnToShow.addMouseEvents();
			DoOperations.hideShow(btnToShow, 1);
			var panelToHide: PanelAbstractLabel = [this.panelScores, this.panelHelp][value];
			var panelToShow: PanelAbstractLabel = [this.panelHelp, this.panelScores][value];
			this.setChildIndex(panelToShow, this.numChildren - 1);
			panelToHide.stop();
			panelToShow.initShow();
			DoOperations.hideShow(panelToHide, 0);
			DoOperations.hideShow(panelToShow, 1, 10, this.startPanelAfterShow, [panelToShow]);
		}
		
		private function startPanelAfterShow(panelToStart: PanelAbstractLabel): void {
			panelToStart.start();
		}
		
		private function removePanelHelpScores(): void {
			var panelToDestroy: PanelAbstractLabel = [this.panelHelp, this.panelScores][this.isShowHelpScores];
			panelToDestroy.destroy();
			for (var strPanel: String in this.objPanel) {
				var panel: DisplayObject = DisplayObject(this.objPanel[strPanel]);
				this.removeChild(panel);
				panel = null;
			}
		}
		
		private function createBtnStage(): void {
			this.btnStage = new Shape();
			this.btnStage.graphics.beginFill(0xFFFFFF, 0);
			this.btnStage.graphics.drawRect(0, 0, 1, 1);
			this.btnStage.graphics.endFill();
			this.addChildAt(this.btnStage, 0);
		}
		
		private function removeBtnStage(): void {
			this.removeChild(this.btnStage);
		}
		
		private function closeAndStartOrContinueGame(e: BtnHitEvent): void {
			this.removeMouseEventsAndStop();
			DoOperations.hideShow(this, 0, 10, this.finishCloseAndStartOrContinueGame);
		}
		
		private function removeMouseEventsAndStop(): void {
			this.btnPlay.removeMouseEvents();
			if (this.isShowHelpScores == 0) {
				this.btnScores.removeMouseEvents();
				this.panelHelp.stop();
			} else if (this.isShowHelpScores == 1) {
				this.btnHelp.removeMouseEvents();
				this.panelScores.stop();
			}
		}
		
		private function finishCloseAndStartOrContinueGame(): void {
			this.destroy();
			this.dispatchEvent(new Event(Event.CLOSE));
		}
		
		public function draw(dimension: Rectangle = null, skinBg: SkinBg = null, skinTfLabelTitles: SkinTf = null, skinBtnPanel: SkinBtnPanel = null, skinScores: SkinScores = null, skinHelp: SkinHelp = null): void {
			super.drawToBg(skinBg || new SkinBg());
			skinBtnPanel = skinBtnPanel || new SkinBtnPanel();
			this.btnPlay.draw(skinBtnPanel);
			this.btnHelp.draw(skinBtnPanel);
			this.btnScores.draw(skinBtnPanel);
			skinTfLabelTitles = skinTfLabelTitles || new SkinTf();
			skinTfLabelTitles.drawToTf(this.tfLabel, 3);
			this.panelScores.draw(skinTfLabelTitles, skinScores || new SkinScores());
			this.panelHelp.draw(skinTfLabelTitles, skinHelp || new SkinHelp());
			this.setDimension(dimension);
			this.stage.addEventListener(Event.RESIZE, this.onStageResize);
			this.stage.dispatchEvent(new Event(Event.RESIZE));
		}
		
		private function onStageResize(e: Event):void {
			this.setDimension(null);
		}
		
		private function setDimension(dimension: Rectangle = null): void {
			dimension = (dimension && dimension.width && dimension.height) ? dimension : new Rectangle (0, 0, this.stage.stageWidth - 2 * PanelStartEndGame.MARGIN_X_STAGE_DEFAULT,  this.stage.stageHeight - 2 * PanelStartEndGame.MARGIN_Y_STAGE_DEFAULT)
			this.width = dimension.width;
			this.height = dimension.height;
			this.setDimensionBtnStage();
		}

		private function setDimensionBtnStage(): void {
			this.btnStage.width = this.stage.stageWidth;
			this.btnStage.height = this.stage.stageHeight;
			this.btnStage.x = (this.width - this.stage.stageWidth) / 2;
			this.btnStage.y = (this.height - this.stage.stageHeight) / 2;
		}
		
		override public function get width(): Number {
			return super.width;
		}
		
		override public function get height(): Number {
			return super.height;
		}
		
		override public function set width(value: Number): void {
			super.width = value;
			this.tfLabel.x = value - PanelAbstractBg.MARGIN_X_ELEMENTS - this.tfLabel.width;
			this.btnPlay.x = PanelAbstractBg.MARGIN_X_ELEMENTS;
			this.btnHelp.x = Math.round(this.btnPlay.x + this.btnPlay.width + PanelAbstractBg.MARGIN_X_ELEMENTS);
			this.btnScores.x = this.btnHelp.x;
			for (var strPanel: String in this.objPanel) {
				var panel: DisplayObject = DisplayObject(this.objPanel[strPanel]);
				panel.width = value - 2 * PanelAbstractBg.MARGIN_X_ELEMENTS;
			}
			this.x = Math.round((this.stage.stageWidth - value) / 2);
		}
		
		override public function set height(value: Number): void {
			super.height = value;
			this.tfLabel.y = value - PanelAbstractBg.MARGIN_Y_ELEMENTS - (this.btnPlay.height + this.tfLabel.height) / 2;
			this.btnPlay.y = Math.round(this.btnHelp.y = value - PanelAbstractBg.MARGIN_Y_ELEMENTS - this.btnPlay.height);
			this.btnScores.y = this.btnPlay.y;
			for (var strPanel: String in this.objPanel) {
				var panel: DisplayObject = DisplayObject(this.objPanel[strPanel]);
				panel.height = value - 2 * PanelAbstractBg.MARGIN_Y_ELEMENTS - this.btnPlay.height - PanelAbstractBg.MARGIN_Y_ELEMENTS;
			}
			this.y = Math.round((this.stage.stageHeight - value) / 2);
		}
		
		override public function destroy(): void {
			this.stage.removeEventListener(Event.RESIZE, this.onStageResize);
			super.destroy();
			this.removeTfLabel();
			this.removeBtns();
			this.removePanelHelpScores();
			this.removeBtnStage();
		}
		
	}

}